var app = getApp();

Page({
  data: {
    expressId: null,
    currentTab: null,
    express: {},
    displayFinlish: false,
    displayCancel: false,
    items: [
      {name: '1001', value: '已发布', checked: false, disabled: true},
      {name: '1002', value: '已接受', checked: false, disabled: true},
      {name: '1003', value: '已完成', checked: false, disabled: true},
      {name: '1004', value: '已取消', checked: false, disabled: true}
    ],
  },
  onLoad(query) {
    console.log(query)
    this.setData({
      expressId: query.expressId,
      currentTab: query.currentTab
    })
  },
  onReady() {
    // if(this.data.currentTab == 0){
    //   this.setData({
    //     display: true
    //   })
    // } else if(this.data.currentTab == 1){
    //   this.setData({
    //     display: true
    //   })
    // }
  },
  onShow() {
    let items = this.data.items
    for(let i=0; i<items.length; i++){
      items[i].checked = false
      this.setData({
        items: items
      })
    }
    this.getExpress()
  },
  changeOrderStatus() {
    let items = this.data.items
    for(let i=0; i<items.length; i++){
      if(items[i].name == this.data.express.orderStatus){
        items[i].checked = true
        this.setData({
          items: items
        })
      }
    }
    if(this.data.express.orderStatus != 1001){
      this.setData({
        display: false
      })
    }
  },
  cancelExpress() {
    dd.httpRequest({
      url: app.globalData.url + '/express/cancel',
      method: 'POST',
      data: {
        expressId: this.data.express.expressId
      },
      dataType: 'json',
      success: (res) => {
        dd.showToast({
          type: 'success',
          content: '取消成功',
          duration: 2000,
          success: () => {
            dd.switchTab({
              url: '/pages/order/order'
            })
          }
        })
      },
      fail: (res) => {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: (res) => {
        
      }
    });
  },
  finishExpress() {
    dd.httpRequest({
      url: app.globalData.url + '/express/finish',
      method: 'POST',
      data: {
        expressId: this.data.express.expressId
      },
      dataType: 'json',
      success: (res) => {
        dd.showToast({
          type: 'success',
          content: '完成成功',
          duration: 2000,
          success: () => {
            dd.switchTab({
              url: '/pages/order/order'
            })
          }
        })
      },
      fail: (res) => {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: (res) => {
        
      }
    });
  },
  getExpress() {
    dd.httpRequest({
      url: app.globalData.url + '/express/get',
      data: {
        expressId: this.data.expressId
      },
      dataType: 'json',
      success: (res) => {
        this.setData({
          express: res.data.data
        })
        this.changeOrderStatus()
        if(this.data.currentTab == 0 && this.data.express.orderStatus == 1001){
          this.setData({
            displayCancel: true
          })
        } else if(this.data.currentTab == 1 && this.data.express.orderStatus == 1002){
          this.setData({
            displayFinlish: true
          })
        }
      },
      fail: (res) => {
        console.log("httpRequestFail---", res)
        dd.alert({ content: JSON.stringify(res) });
      },
      complete: (res) => {
        dd.hideLoading();
      }
    });
  }
});
